package org.duiduo.controller;

import org.duiduo.api.bean.BookVO;
import org.duiduo.api.request.BookDeleteBatchRequest;
import org.duiduo.api.request.BookFindPageRequest;
import org.duiduo.api.valid.ValidationGroups;
import org.duiduo.api.response.Result;
import org.duiduo.service.BookService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 书籍控制层
 *
 * @author liangze
 */
@RestController
@RequestMapping("/book")
public class BookController extends BaseController {

    @Resource
    private BookService bookService;

    /**
     * 分页列表
     *
     * @param request
     * @return
     */
    @PostMapping("/query")
    public Result<?> findPage(@RequestBody @Valid BookFindPageRequest request) {
        return Result.success(bookService.findPage(request));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        return Result.success(bookService.deleteById(id));
    }

    /**
     * 新增
     *
     * @param bookVO
     * @return
     */
    @PostMapping
    public Result<?> save(@RequestBody @Validated(value = {ValidationGroups.Insert.class}) BookVO bookVO) {
        return Result.success(bookService.save(bookVO));
    }

    /**
     * 编辑修改
     *
     * @param bookVO
     * @return
     */
    @PutMapping
    public Result<?> update(@RequestBody @Validated(value = {ValidationGroups.Update.class}) BookVO bookVO) {
        return Result.success(bookService.update(bookVO));
    }

    /**
     * 批量删除
     *
     * @param request
     * @return
     */
    @PostMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestBody @Valid BookDeleteBatchRequest request) {
        return Result.success(bookService.deleteBatch(request));
    }
}
